package com.fjec.common.util;

import java.math.BigDecimal;
import java.text.NumberFormat;

public class NumberUtil {

    private static NumberFormat numberFormat = NumberFormat.getInstance();

    /**
     * 整数转字节数组
     *
     * @param number
     * @param endian 0=大端序默认 1=小端序
     * @return
     */
    public static byte[] int2byte(int number, int endian) {
        byte[] result = new byte[4];
        // 由高位到低位 大端序
        if (endian == 0) {
            result[0] = (byte) ((number >> 24) & 0xFF);
            result[1] = (byte) ((number >> 16) & 0xFF);
            result[2] = (byte) ((number >> 8) & 0xFF);
            result[3] = (byte) (number & 0xFF);
        } else {
            result[3] = (byte) ((number >> 24) & 0xFF);
            result[2] = (byte) ((number >> 16) & 0xFF);
            result[1] = (byte) ((number >> 8) & 0xFF);
            result[0] = (byte) (number & 0xFF);
        }
        return result;
    }

    /**
     * 字节数组转整数
     *
     * @param bytes
     * @param endian 0=大端序默认 1=小端序
     * @return
     */
    public static int byte2int(byte[] bytes, int endian) {
        int result = 0;
        // &0xff将byte值无差异转成int,避免Java自动类型提升后,会保留高位的符号位
        if (endian == 0) {
            int v0 = (bytes[0] & 0xFF) << 24;
            int v1 = (bytes[1] & 0xFF) << 16;
            int v2 = (bytes[2] & 0xFF) << 8;
            int v3 = (bytes[3] & 0xFF);
            result = v0 | v1 | v2 | v3;
        } else {
            int v0 = (bytes[3] & 0xFF) << 24;
            int v1 = (bytes[2] & 0xFF) << 16;
            int v2 = (bytes[1] & 0xFF) << 8;
            int v3 = (bytes[0] & 0xFF);
            result = v0 | v1 | v2 | v3;
        }
        return result;
    }

    /**
     * 格式化浮点数
     *
     * @param number
     * @param fractionDigits
     * @param groupingUsed
     * @return
     */
    public static String formatDouble(double number, int fractionDigits, boolean groupingUsed) {
        //设置保留多少位小数
        numberFormat.setMaximumFractionDigits(fractionDigits);
        // 取消科学计数法
        numberFormat.setGroupingUsed(groupingUsed);
        //返回结果
        return numberFormat.format(number);
    }

    public static String num2Bin(Integer num) {
        return Integer.toBinaryString(num);
    }

    public static String num2Hex(Integer num) {
        return Integer.toHexString(num);
    }

    /**
     * 所有位放弃某个数字
     *
     * @param num
     * @param giveUpNum
     * @return
     */
    public static long giveUpNum(long num, int giveUpNum) {
        long result = num;
        long tmp = num;
        int i = 0;
        do {
            if ((tmp % 10) == giveUpNum) {
                result = result + (long) Math.pow(10, i);
            }
            tmp = tmp / 10;
            i++;
        } while (tmp > 0); // 当tmp小于等于0退出
        return result;
    }

    /**
     * 除法
     *
     * @param strNum1
     * @param strNum2
     * @return
     */
    public BigDecimal divide(String strNum1, String strNum2) {
        BigDecimal num1 = new BigDecimal(strNum1);
        BigDecimal num2 = new BigDecimal(strNum2);
        return num1.divide(num2);
    }

    public static void main(String[] args) {
//        int a = 0b1000;
//        int a = Integer.MAX_VALUE;
        int a = 8752;
        byte[] bs = NumberUtil.int2byte(a, 0);
        for (byte b : bs) {
            System.out.println(b);
        }
        int b = NumberUtil.byte2int(bs, 0);
        System.out.println(b);

    }
}
